package com.qing.microservices.shirostudy.controller;

import com.qing.microservices.shirostudy.common.utils.CryptoUtil;
import com.qing.microservices.shirostudy.common.utils.SaltUtil;
import com.qing.microservices.shirostudy.entity.User;
import com.qing.microservices.shirostudy.service.IUserService;
import com.qing.microservices.shirostudy.vo.UserPasswdResetVO;
import com.qing.microservices.shirostudy.vo.UserRegistryVO;
import com.qing.microservices.shirostudy.vo.UsernameVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Q
 * @since 2023-06-06
 */
@Slf4j
@Controller
@RequestMapping("/user")
public class UserController {

    @Resource
    private IUserService userService;

    @RequestMapping(value = "/login")
    public String login(UsernameVO user) {
        try {
            Subject subject = SecurityUtils.getSubject();
            subject.login(new UsernamePasswordToken(user.getUsername(), user.getPasswd()));
            return "redirect:/index";
        } catch (UnknownAccountException e) {
            log.error("用户名[{}]不存在", user.getUsername(), e);
        } catch (IncorrectCredentialsException e) {
            log.error("用户名或密码错误", e);
        } catch (AuthenticationException e) {
            log.error("认证错误", e);
        }
        return "redirect:/login/login";
    }

    @PostMapping("/register")
    public String register(@RequestBody UserRegistryVO registry) {

        User user = new User();
        user.setUsername(registry.getUsername());
        user.setAge(registry.getAge());
        String salt = SaltUtil.getSalt(8);
        user.setPasswd(CryptoUtil.encryptPasswd(registry.getPasswd(), salt));
        user.setSalt(salt);
        LocalDateTime now = LocalDateTime.now();

        user.setCreateTime(now);
        user.setUpdateTime(now);

        userService.save(user);
        return "login";
    }

    @PostMapping("/reset-passwd")
    public String resetPasswd(@RequestBody UserPasswdResetVO pwdReset) {
        User user = userService.queryByUsername(pwdReset.getUsername());
        if (user != null) {
            String salt = SaltUtil.getSalt(8);
            user.setPasswd(CryptoUtil.encryptPasswd(pwdReset.getNewPasswd(), salt));
            user.setSalt(salt);
            boolean b = userService.updateById(user);
            if (b) {
                log.info("重置密码成功");
                return "login";
            }
        }
        return "redirect:error/500";
    }

    @RequestMapping("logout")
    public String logout() {
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        log.info("登出成功");
        return "redirect:/login/login";
    }
}
